package com.yiudishishizijie;

import com.yidushishizijie.dao.StudentDao;
import com.yidushishizijie.entity.Student;
import com.yidushishizijie.utils.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MybatisTest02 {

    @Test
    public void test01() throws IOException {
        SqlSession session = MyBatisUtil.getSqlSession();
        StudentDao studentDao = session.getMapper(StudentDao.class);
        List studentList = studentDao.selectStudents();
        studentList.forEach(stu -> System.out.println(stu));
    }


    @Test
    public void test02(){
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        StudentDao dao = sqlSession.getMapper(StudentDao.class);
        Student student = new Student();
        student.setId(1009);
        student.setAge(23);
        student.setName("你看");
        student.setEmail("sdaf@123.com");
        int num = dao.insertStudent(student);
        sqlSession.commit();
        System.out.println("添加数量对象"+num);
    }

    @Test
    public void test03(){
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
        Map<String,Object> data = new HashMap<String,Object>();
        data.put("myname","张三");
        data.put("myage",23);
        List<Student> studentList = studentDao.selectByNameAndAge(data);
        sqlSession.commit();
        for (int i = 0; i < studentList.size(); i++) {
            Student student = studentList.get(i);
            System.out.println(student);
        }
    }

    @Test
    public void test04() {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
        List<Student> studentList = studentDao.selectUseResultMap(23);
        sqlSession.commit();
        for (int i = 0; i < studentList.size(); i++) {
            Student student = studentList.get(i);
            System.out.println(student);
        }
    }

}
